' Mouse Action Shooter - Target Practice.txt b+ 2021-03-18 from
' _Title "Mouse Action Shooter #2 Targets fix" 'B+ 2019-04-04
' 2019-04-03 add some targets for practice
' Update Mouse Action Shooter with MB triggers 2019-04-01
' Mouse Action Shooter started from eRATication 5 by bplus 2018-08-06"

' SCORE System\
' Left click to fire a bullet, they cost 10 points each.
' Targets range from 30 for biggest targets to 100 points for smallest Targets
' Get hit by target loose 100 points and delays game with explosion.

NBullets = 15
BSpeed = 50
NTargets = 3 
TSpeed = 10 
ShooterRadius = 50
Score = 0

restart\
flaghit = 0
' targets init, ti = target index all thru app
ti = 0
[
	ti = a[ti,1]
	jmp gt[ti,NTargets]
	gs NewTarget\
]
' init bLive array, bi = bullet index all thru app
bi = 0 
[
	bi = a[bi,1]
	jmp gt[bi,NBullets]
	set bx;bi;0
	set by;bi;0
	set bdx;bi;0
	set bdy;bi;0
	set bLive;bi;0
]

' shooter or ship, lastX, LastY was the last position of shooter
shooterX = d[xmax,2]
shootery = d[ymax,2]
shooterA = 0
lastx = d[xmax,2]
lasty = d[ymax,2]
gs drawshooter\

' game, mouse display off
mouse 0
last1 = Timer[]
[
    cls
	'display score
	ink 255;255;0
	paper 0;0;0;0
	cpr 21;score
	'get mouse
    poll
    shooterX = MouseX
	shooterY = MouseY
	'detect mouse movement from last position, point nose to direction of movement
    If or[gt[abs[s[lastx,shooterX]],2],gt[Abs[s[lasty,shooterY]],2]]
        shooterA = atan2[s[shooterY,lasty],s[shooterX,lastx]]
        lastx = shooterX
		lasty = shooterY
    fi
	'check for bullet firing Left mouse
    t = Timer[]
    if mouseL
		if gt[s[t,last1],.15]		
			mb1 = 1
			last1 = t
		fi
    fi
	'updates targets, shooter and bullets
    gs handleTargets\
    gs drawshooter\
    gs handleBullets\
	' collision between target and shooter, explosion
	if expX
		expRadius = 0
		show 1
		[
			ink s[255,x[.5,expRadius]];s[255,x[expRadius,3]];0;8
			fcirc expX;expY;expRadius
			expRadius = a[expRadius,1]
			jmp gt[expRadius,expR]
			show 1
			wait .005
		]
		expX = 0
	el
	    show 1
		wait .03
	fi
]
end

NewTarget\
    'pick edge, ti is target index
    edge = Int[Rnd[4]]
	if eq[edge,0]
		set tX;ti;s[0,ShooterRadius]
		set tY;ti;a[rnd[s[ymax,300]],150]
		set tA;ti;a[pi[1.5],rnd[pi[1]]]
	ei eq[edge,1]
		set tX;ti;a[xmax,ShooterRadius]
		set tY;ti;a[rnd[s[ymax,300]],150]
		set tA;ti;a[pi[.5],rnd[pi[1]]]		
	ei eq[edge,2]
		set tX;ti;rnd[xmax]
		set tY;ti;s[0,ShooterRadius]
		set tA;ti;rnd[pi[1]]	
	ei eq[edge,3]
		set tX;ti;rnd[xmax]
		set tY;ti;a[ymax,ShooterRadius]
		set tA;ti;a[pi[1],rnd[pi[1]]]
	fi	
	set tR;ti;x[a[int[rnd[8]],3],10]
rtn

handleTargets\
	'ti is target index used in all the gosubs gs
	ti = 1
	[
		set tX;ti;a[get[tX,ti],x[tSpeed,cos[get[tA,ti]]]]
		set tY;ti;a[get[tY,ti],x[tSpeed,sin[get[tA,ti]]]]
        'inbounds? if not set new
		If lt[get[tx,ti],x[-1,shooterRadius]]
			gs NewTarget\
		ei gt[get[tx,ti],a[xmax,shooterRadius]]
			gs NewTarget\
		ei lt[get[ty,ti],x[-1,shooterRadius]]
			gs NewTarget\
		ei gt[get[ty,ti],a[ymax,shooterRadius]]
			gs NewTarget\
		el	
			gs hitShooter\
			if hitShooter
				score = s[score,100]
				beep
				'note explosion coordinates but finish drawing things
				expX = d[a[shooterX,get[tx,ti]],2]
				expY = d[a[shooterY,get[ty,ti]],2]
				expR = x[1.3,get[tr,ti]]
				'now reassign target coordinates
				gs drawTarget\
				gs NewTarget\
			el
				gs drawTarget\
			fi
		fi
		ti = a[ti,1]
		jmp gt[ti,NTargets]
	]
rtn

drawTarget\
	'ti is target index used in all gosubs for Target
	drawTargetR = get[tR,ti]
	drawTargetStep = d[s[0,drawTargetR],10]
	drawTargetCount = 0
	[
		drawTargetCount = a[drawTargetCount,1]
		if m[drawTargetCount,2]
			ink 255;0;0 
		El
			ink 255;255;255
		fi
		fcirc get[tX,ti];get[tY,ti];drawTargetR
		drawTargetR = a[drawTargetR,drawTargetStep]
		jmp lte[drawTargetR,0]
    ]
rtn

HandleBullets\
	bi = 0
	[
		bi = a[bi,1]
		jmp gt[bi,NBullets]
		' have a bullet slot and an order to fire?
		if and[eq[get[bLive,bi],0],eq[mb1,1]]
			set bx;bi;a[shooterX,x[.5,shooterRadius,cos[shooterA]]]
			set by;bi;a[shooterY,x[.5,shooterRadius,sin[shooterA]]]
			set bdx;bi;x[bSpeed,cos[shooterA]]
			set bdy;bi;x[bSpeed,sin[shooterA]]
			set bLive;bi;1
			mb1 = 0
			'bullets cost 10 points
			score = s[score,10] 
		ei eq[get[bLive,bi],1]
			' Then new location
			set bx;bi;a[get[bx,bi],get[bdx,bi]]
			set by;bi;a[get[by,bi],get[bdy,bi]]
			' in bounds?
			'assume it's out
			set bLive;bi;0
			if gt[get[bx,bi],-50]
				if lt[get[bx,bi],a[xmax,50]]
					if gt[get[by,bi],-50]
						if lt[get[by,bi],a[ymax,50]]
							' it's Alive!
							set bLive;bi;1
							'check for collision with targets
							gs HitTarget\
							if gt[hitTarget,0]
								score = a[score,s[130,get[tr,hitTarget]]]
								set bLive;bi;0
								ti = hitTarget
								gs NewTarget\
							El
								'draw bullet
								ink 255;255;0
								fcirc get[bx,bi];get[by,bi];4
							fi
						fi
					fi
				fi
			fi
		fi
	]
rtn

HitTarget\ 
	hitTarget = 0
	ti = 0
	[
		ti = a[ti,1]
		jmp gt[ti,NTargets]
		If lte[sqr[a[p[s[get[tx,ti],get[bx,bi]],2],p[s[get[ty,ti],get[by,bi]],2]]],get[tr,ti]]
			hitTarget = ti
		fi
	]
rtn

hitShooter\ 
	'ti is target index for all target gosubs
	hitShooter = 0
	If lte[sqr[a[p[s[shooterX,get[tX,ti]],2],p[s[shooterY,get[tY,ti]],2]]],a[10,get[tR,ti]]]
		hitShooter = ti
	fi
rtn

drawshooter\
    x1 = a[shooterX,x[s[shooterRadius,30],cos[shooterA]]]
    y1 = a[shooterY,x[s[shooterRadius,30],sin[shooterA]]]
    x2 = a[shooterX,x[a[shooterRadius,20],cos[a[shooterA,pi[d[11,16]]]]]]
    y2 = a[shooterY,x[a[shooterRadius,20],sin[a[shooterA,pi[d[11,16]]]]]]
    x3 = a[shooterX,x[a[shooterRadius,20],cos[s[shooterA,pi[d[11,16]]]]]]
    y3 = a[shooterY,x[a[shooterRadius,20],sin[s[shooterA,pi[d[11,16]]]]]]
	ink 0;130;60
    ftri x1;y1;x2;y2;x3;y3
    x1 = a[shooterX,x[shooterRadius,cos[shooterA]]]
    y1 = a[shooterY,x[shooterRadius,sin[shooterA]]]
    x2 = a[shooterX,x[shooterRadius,cos[a[shooterA,pi[d[7,8]]]]]]
    y2 = a[shooterY,x[shooterRadius,sin[a[shooterA,pi[d[7,8]]]]]]
    x3 = a[shooterX,x[shooterRadius,cos[s[shooterA,pi[d[7,8]]]]]]
    y3 = a[shooterY,x[shooterRadius,sin[s[shooterA,pi[d[7,8]]]]]]
	ink 0;0;200
    ftri x1;y1;x2;y2;x3;y3
    x2 = a[shooterX,x[shooterRadius,cos[a[shooterA,pi[d[15,16]]]]]]
    y2 = a[shooterY,x[shooterRadius,sin[a[shooterA,pi[d[15,16]]]]]]
    x3 = a[shooterX,x[shooterRadius,cos[s[shooterA,pi[d[15,16]]]]]]
    y3 = a[shooterY,x[shooterRadius,sin[s[shooterA,pi[d[15,16]]]]]]
	ink 255;255;255
    ftri x1;y1;x2;y2;x3;y3
rtn
